package com.cloud.tsp.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.api.domain.alarm.VinLive;
import com.cloud.api.domain.tsp.QueryVehicle;
import com.cloud.api.domain.tsp.QueryVehicleMap;
import com.cloud.api.domain.tsp.VehicleSale;
import com.cloud.api.domain.tsp.VinLockStatus;
import com.cloud.core.exception.MyException;
import com.cloud.mybatis.domain.TableDataInfo;
import com.cloud.tsp.config.MysqlConfig;
import com.cloud.tsp.mapper.VinLiveMapper;
import com.cloud.tsp.service.IVehicleMessageDailyService;
import com.cloud.tsp.service.IVehicleSaleService;
import com.cloud.tsp.service.IVinLiveService;
import jakarta.annotation.Resource;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Locale;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author haohaibo
 * @since 2024-09-26
 */
@Service("vinLiveService")
public class VinLiveServiceImpl extends ServiceImpl<VinLiveMapper, VinLive> implements IVinLiveService {

    @Autowired
    private MysqlConfig mysqlConfig;
    @Resource
    private VinLiveMapper vinLiveMapper;
    @Resource
    private MessageSource messageSource;
    @Resource
    private IVehicleSaleService vehicleSaleService;
    @Resource(name = "myRedis")
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private IVehicleMessageDailyService vehicleMessageDailyService;

    @Override
    public TableDataInfo<VinLockStatus> selectVinListTsp(QueryVehicle para) {
        if (StringUtil.isBlank(para.getDeptId())){
            para.setDeptId(null);
        }
        switch (para.getRange()){
            case "all" :
                return selectAllVinListTsp(para);
            case "online" :
                return selectOnlineVinListTsp(para);
            case "offline" :
                return selectOfflineVinListTsp(para);
            default:
                throw new MyException("wds.exception.lineRange");
        }
    }

    @Override
    public String selectVinOnlineStatus(String id) {
        Locale locale = LocaleContextHolder.getLocale();
        VehicleSale byId = vehicleSaleService.getById(id);
//        Double timestamp = redisTemplate.opsForZSet().score("ONLINE-EV-VIN", byId.getVin());
        QueryVehicleMap para = new QueryVehicleMap();
        LocalDateTime now = LocalDateTime.now();
        //修改成30秒就离线
        LocalDateTime twoMinutesAgo = now.minusSeconds(mysqlConfig.getSecond());
        para.setStartTime(twoMinutesAgo);
        para.setVin(byId.getVin());
        Boolean online = vehicleMessageDailyService.isOnline(para);
        if (online) {
            return messageSource.getMessage("vehicle.status.online", null, locale);
        }else {
            return messageSource.getMessage("vehicle.status.offline", null, locale);
        }
        /*Page<VinLockStatus> page =new Page<>();
        page.setCurrent(1);
        page.setSize(10);
        QueryVehicle para = new QueryVehicle();
        LocalDateTime now = LocalDateTime.now();
        para.setStartTime(now.minus(mysqlConfig.getSecond(), ChronoUnit.SECONDS));
        para.setEndTime(now);
        para.setVin(id);
        IPage<VinLockStatus> data = vinLiveMapper.selectLineVinListTsp(page,para);
        long total = data.getTotal();
        String online = messageSource.getMessage("vehicle.status.online", null, "在线", locale);
        String offline = messageSource.getMessage("vehicle.status.offline", null,"离线", locale);
        if (total > 0){
            return online;
        }else {
            return offline;
        }*/
    }

    public TableDataInfo<VinLockStatus> selectAllVinListTsp(QueryVehicle para) {
        Page<VinLockStatus> page =new Page<>();
        page.setCurrent(para.getPage());
        page.setSize(para.getPageSize());
//        QueryVehicle permissions = getPermissions(para);
//        IPage<VinLockStatus> data = vehicleMessageDailyMapper.selectAllVinListTsp(page, permissions);
        IPage<VinLockStatus> data = vinLiveMapper.selectAllVinListTsp(page, para);
        desensitize(data.getRecords());
        return TableDataInfo.build(data);
    }

    public void desensitize(List<VinLockStatus> list){
        for (VinLockStatus vinLockStatus : list) {
            vinLockStatus.setVin(StrUtil.hide(vinLockStatus.getVin(), 8, 11));
        }
    }

    public TableDataInfo<VinLockStatus> selectOnlineVinListTsp(QueryVehicle para) {
        //从数据库取的方案
        Page<VinLockStatus> page =new Page<>();
        page.setCurrent(para.getPage());
        page.setSize(para.getPageSize());
//        QueryVehicle permissions = getPermissions(para);
        QueryVehicle permissions = new QueryVehicle();
        LocalDateTime now = LocalDateTime.now();
//        permissions.setStartTime(now.minus(mysqlConfig.getSecond(), ChronoUnit.SECONDS));
//        permissions.setEndTime(now);
        para.setStartTime(now.minus(mysqlConfig.getSecond(), ChronoUnit.SECONDS));
        para.setEndTime(now);
        IPage<VinLockStatus> data = vinLiveMapper.selectLineVinListTsp(page,para);
        desensitize(data.getRecords());
        return TableDataInfo.build(data);
    }

    public TableDataInfo<VinLockStatus> selectOfflineVinListTsp(QueryVehicle para) {
        //从数据库取的方案
        Page<VinLockStatus> page =new Page<>();
        page.setCurrent(para.getPage());
        page.setSize(para.getPageSize());
//        QueryVehicle permissions = getPermissions(para);
        QueryVehicle permissions = new QueryVehicle();
        LocalDateTime now = LocalDateTime.now();
//        permissions.setEndTime(now.minus(mysqlConfig.getSecond(), ChronoUnit.SECONDS));
        para.setEndTime(now.minus(mysqlConfig.getSecond(), ChronoUnit.SECONDS));
        IPage<VinLockStatus> data = vinLiveMapper.selectLineVinListTsp(page,para);
        desensitize(data.getRecords());
        return TableDataInfo.build(data);
    }

}